﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QuanLyThi.TaoDeThi
{
    public partial class FrmThemCauHoi : Form
    {

        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);

        int Ms_Mondaotao = 0;
        DataTable dt = null;


        public FrmThemCauHoi(int Ms_Mondaotao,DataTable dt)
        {
            InitializeComponent();
            this.Ms_Mondaotao = Ms_Mondaotao;
            this.dt = dt;
            this.Load();
        }

        void Load()
        {
            msql = "Select id_MaDeThi From tb_MaDeThi where MS_Mondaotao=" + Ms_Mondaotao;
            DataTable dt = mLibrary.GetDataTable(msql, "a");
            cboMaDe.DataSource = dt;
            cboMaDe.DisplayMember = "id_MaDeThi";
            cboMaDe.ValueMember = "id_MaDeThi";
        }

        private void butTH_Click(object sender, EventArgs e)
        {
            try
            {
                //them ma de
                msql = "insert into dbo.tb_MaDeThi(MS_Mondaotao) " +
                       " values (" + Ms_Mondaotao + ") select SCOPE_IDENTITY()";
                int MaDe=mLibrary.Timso_int(msql);
               //Them cau hoi
                foreach (DataRow row in dt.Rows)
                {
                    int chon = int.Parse( row["Chon"].ToString());
                    if (chon == 1)
                    {
                        int CauHoi = int.Parse(row["CauHoi"].ToString());
                        if (row["idCauHoiCha"].ToString() == "")//ko co cau hoi cha
                        {
                            msql = "insert into dbo.tb_MaDeChiTiet (id_MaDeThi,Cauhoi)" +
                                   " values (" + MaDe + "," + CauHoi + ")";
                            mLibrary.RunProc(msql);
                        }
                        else
                        {
                            int IDCauHoiCha = int.Parse(row["IDCauHoiCha"].ToString());
                            msql = "insert into dbo.tb_MaDeChiTiet (id_MaDeThi,Cauhoi,CauHoiCha,IDCauHoiCha)" +
                                  " values (" + MaDe + "," + CauHoi + ",1," + IDCauHoiCha + ")";
                            mLibrary.RunProc(msql);
                        }
                    }
                }

                MessageBox.Show("Thêm thông tin thành công", "Thông báo");
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butTHeoMade_Click(object sender, EventArgs e)
        {
            try
            {
                int MaDe = int.Parse (cboMaDe.SelectedValue.ToString());
                //Them cau hoi
                foreach (DataRow row in dt.Rows)
                {
                    int chon = int.Parse(row["Chon"].ToString());
                    if (chon == 1)
                    {
                        int CauHoi = int.Parse(row["CauHoi"].ToString());
                        if (row["idCauHoiCha"].ToString() == "")//ko co cau hoi cha
                        {
                            msql = "insert into dbo.tb_MaDeChiTiet (id_MaDeThi,Cauhoi)" +
                                   " values (" + MaDe + "," + CauHoi + ")";
                            mLibrary.RunProc(msql);
                        }
                        else
                        {
                            int IDCauHoiCha = int.Parse(row["IDCauHoiCha"].ToString());
                            msql = "insert into dbo.tb_MaDeChiTiet (id_MaDeThi,Cauhoi,CauHoiCha,IDCauHoiCha)" +
                                  " values (" + MaDe + "," + CauHoi + ",1," + IDCauHoiCha + ")";
                            mLibrary.RunProc(msql);
                        }
                    }
                }

                MessageBox.Show("Thêm thông tin thành công","Thông báo");
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
